<?php

namespace app\controller\open;

use app\validate\PageValidate;
use app\validate\OssProductValidate;
use app\service\OpenAppProductService;
use share\service\ContextService;

class OssProductController extends BaseOpenController{

    protected $beforeActionList = [
        "checkOpenAppSign",
    ];

    public function initValidate(){
        $this->validate = new OssProductValidate();
    }

    public function list_action(){
        $page = $this->check('pageInfo',null,(new PageValidate()));
        $openAppId = $this->openApp['id'];
        $data = OpenAppProductService::getProductsByAppId($openAppId,$page['page'],$page['size']);
        return self::ok(self::formatPageData($page,$data['total'],$data['list']));
    }

    public function get_free_action(){
        $openAppId = $this->openApp['id'];
        $input = $this->check();
        $data = OpenAppProductService::getFreeProduct($openAppId,$input['sn'],$input['sku'],$input['user_id']);
        return self::ok($data);
    }

    public function buy_free_action(){
        $openAppId = $this->openApp['id'];
        $input = $this->check('getFree');
        $data = OpenAppProductService::buyFreeProduct($openAppId,$input['sn'],$input['sku'],$input['user_id']);
        return self::ok($data);
    }

}